package ui.panel {

	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.geom.Point;
	
	import globals.Global;
	
	import ui.component.Button;
	import ui.panel.cell.OptionCell;
	
	public class OptionPanel extends BasePanel {
		
		private var buildCell:OptionItem;
		private var optionPoint:Point;
		private var optionConfig:Array;
		private var optionList:Array;

		public function OptionPanel() {
			super();
			pid = PanelType.OPTION_PANEL;
		}
		
		override protected function initUI():void {
			bg = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("circle"));
			bg.x = -bg.width / 2;
			bg.y = -bg.height / 2;
			addChildAt(bg, 0);
			
			optionConfig = new Array();
			optionConfig[0] = ["circle_build", cancelHandler];
			optionConfig[1] = ["circle_build", buildHandler];
			optionConfig[2] = ["circle_build", infoHandler];
			optionList = new Array();
		}
		
		private function cancelHandler(item:OptionItem):void {
			close();
		}
		
		private function buildHandler(item:OptionItem):void {
			Global.uiManager.openPanel(PanelType.TOWER_PANEL);
			Global.uiManager.towerPanel.buildPoint = optionPoint;
			close();
		}
		
		private function infoHandler(item:OptionItem):void {
			trace("click info");
		}
		
		private function upgradeHandler():void {
		}
		
		public function openByPoint(point:Point):void {
			x = point.x;
			y = point.y;
			optionPoint = point;
//			Global.uiManager.showMaskLayer();
			Global.game.panelLayer.addChild(this);
			opened = true;
			animateShow();
			
			var options:Array = [0, 1, 0, 1];
			var item:OptionItem;
			var data:Array;
			var gap:uint = 360 / options.length;
			var radius:uint = 66;
			for (var i:int = 0; i<options.length; i++) {
				data = optionConfig[options[i]];
				if (data != null) {
					item = new OptionItem(data[0], data[1]);
					item.x = Math.cos(Math.PI * (90 + gap * i) / 180) * radius;
					item.y = Math.sin(Math.PI * (90 + gap * i) / 180) * radius;
					addChild(item);
					optionList.push(item);
				}
			}
		}
		
		override public function close():void {
			super.close();
			animateHide();
//			Global.uiManager.hideMaskLayer();
		}
		
		public function animateShow():void {
			scaleX = 0
			scaleY = 0;
			TweenLite.to(this, 0.1, {scaleX:1, scaleY:1});
		}
		
		public function animateHide():void {
			TweenLite.to(this, 0.1, {scaleX:0, scaleY:0});
		}
		
		public function clear():void {
			
		}
	}
}

import flash.display.Bitmap;
import flash.display.BitmapData;

import globals.Global;

import ui.component.Button;

class OptionItem extends Button {
	
	private var icon:Bitmap;
	
	public function OptionItem(iconUrl:String, handler:Function) {
		super(Global.assetsManager.getAssetsAsBitmapData("circle_icon_off"), 
			Global.assetsManager.getAssetsAsBitmapData("circle_icon_on"), 
			Global.assetsManager.getAssetsAsBitmapData("circle_icon_off"), handler);
		
		skin.x = -width * 0.5;
		skin.y = -height * 0.5;

		icon = new Bitmap(Global.assetsManager.getAssetsAsBitmapData(iconUrl));
		icon.x = -width * 0.5;
		icon.y = -height * 0.5;
		addChild(icon);
	}
}